<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>ObjectStore Search Tests</title>
		
		<script type="text/javascript" src="../lib/jsunit/app/jsUnitCore.js"></script>
		<script type="text/javascript" src="../lib/prototype/prototype.js"></script>
		<script type="text/javascript" src="../lib/gears/gears_init.js"></script>
		<script type="text/javascript" src="../src/ObjectThought.js"></script>
		<script type="text/javascript" src="testclasses.js"></script>
		<script type="text/javascript">//<![CDATA[
			function setUp()
			{
				var store = ObjectStore.getStore("objectthought_unittests");
				for (var i = 0; i < 10; i++)
				{
					var validationReport = store.saveObject(new SimpleClass("A Value"));
					assertTrue(validationReport.toString(), validationReport.passes());
				}
			}
			
			function testRetrevingOfSingleObjectUsingSimpleSearchObject()
			{
				var store = ObjectStore.getStore("objectthought_unittests");
				
				//SetUp
				var simpleClassObject = new SimpleClass("A Search Value");
				var validationReport = store.saveObject(simpleClassObject);
				assertTrue(validationReport.toString(), validationReport.passes());
				
				//Creating Search Object
				var searchObject = { id:simpleClassObject.id, dataValue:"A Search Value" };
				var objectFromStore = store.findObject(SimpleClass, searchObject);
				assertNotNull("Object should not be null", objectFromStore);
				assertEquals(objectFromStore.dataValue, "A Search Value");
				assertEquals(objectFromStore.id, simpleClassObject.id);
			}
			
			function testRetrevingOfMultipleObjectsUsingSimpleSearchObject()
			{
				var store = ObjectStore.getStore("objectthought_unittests");
				
				//Set Up
				var returnCount = 3;
				for (var i = 0; i < returnCount; i++)
				{
					var validationReport = store.saveObject(new SimpleClass("A Search Value"));
					assertTrue(validationReport.toString(), validationReport.passes());
				}
				
				//Creating Search Object
				var searchObject = { dataValue:"A Search Value" };
				var objectFromStore = store.findObjects(SimpleClass, searchObject);
				assertNotNull("Object should not be null", objectFromStore);
				assertTrue("Returned object should be an Array", Object.isArray(objectFromStore));
				assertEquals(objectFromStore.length, returnCount);
				
				for (var i = 0; i < objectFromStore.length; i++)
				{
					assertEquals(objectFromStore[i].dataValue, "A Search Value");
				}
			}
			
			function testSearchWithComplexSearchObjectUsingArray()
			{
				var store = ObjectStore.getStore("objectthought_unittests");
				
				//Set Up
				var idArray = [];
				for (var i = 0; i < 4; i++)
				{
					var simpleClass = new SimpleClass("A Value");
					var validationReport = store.saveObject(simpleClass);
					assertTrue(validationReport.toString(), validationReport.passes());
					idArray.push(simpleClass.id - (i * 2)); //Spice it up
				}
				
				//Creating Search Object
				var searchObject = { id:idArray };
				var objectFromStore = store.findObjects(SimpleClass, searchObject);
				assertNotNull("Object should not be null", objectFromStore);
				assertTrue("Returned object should be an Array", Object.isArray(objectFromStore));
				assertEquals(idArray.length, objectFromStore.length);
				
				for (var i = 0; i < objectFromStore.length; i++)
				{
					assertContains(objectFromStore[i].id, idArray)
				}
			}
			
			function testSearchWithComplexSearchObjectUsingRange()
			{
				var store = ObjectStore.getStore("objectthought_unittests");
				
				//Set Up
				var simpleClassArray = [];
				for (var i = 0; i < 6; i++)
				{
					var simpleClass = new SimpleClass("A Search Value");
					var validationReport = store.saveObject(simpleClass);
					assertTrue(validationReport.toString(), validationReport.passes());
					simpleClassArray.push(simpleClass);
				}
				
				//Creating Search Object
				var idRange = $R(simpleClassArray[0].id, simpleClassArray[simpleClassArray.length - 1].id);
				var searchObject = { id:idRange };
				var objectFromStore = store.findObjects(SimpleClass, searchObject);
				assertNotNull("Object should not be null", objectFromStore);
				assertTrue("Returned object should be an Array", Object.isArray(objectFromStore));
				assertEquals($A(idRange).length, objectFromStore.length);
				
				for (var i = 0; i < objectFromStore.length; i++)
				{
					assertTrue("Object is not contained in Range.", idRange.include(objectFromStore[i].id));
				}
			}
			
			function testSearchWithComplexSearchObjectUsingValidationFunction()
			{
				var store = ObjectStore.getStore("objectthought_unittests");
				
				//Set Up
				var returnCount = 3;
				for (var i = 0; i < returnCount; i++)
				{
					var validationReport = store.saveObject(new SimpleClass("A Search Value"));
					assertTrue(validationReport.toString(), validationReport.passes());
				}
				
				//Creating Search Object
				var searchFunction = function(value) { return value.include("Search"); };
				var searchObject = { dataValue:searchFunction };
				var objectFromStore = store.findObjects(SimpleClass, searchObject);
				assertNotNull("Object should not be null", objectFromStore);
				assertTrue("Returned object should be an Array", Object.isArray(objectFromStore));
				assertEquals(objectFromStore.length, returnCount);
				
				for (var i = 0; i < objectFromStore.length; i++)
				{
					assertTrue("Object did not pass validation function", searchFunction(objectFromStore[i].dataValue));
				}
			}
			
			function tearDown()
			{
				var store = ObjectStore.getStore("objectthought_unittests");
				store.deleteObjects(SimpleClass);
				var simpleClassObjects = store.getObjects(SimpleClass);
				assertArrayEquals("The SimpleClass table should be empty.", simpleClassObjects);
			}
		//]]></script>
	
	</head>
	<body>
	    <p>This is a <a href="http://www.jsunit.net/">JsUnit</a> test page.</p>
	    <p>In order to run these tests, please load this html file in JsUnit's <a href="../lib/jsunit/testRunner.html">testRunner.html</a>.</p>
	</body>
</html>